package cn.bgs.controller;


import cn.bgs.pojo.EmployeeInfo;
import cn.bgs.pojo.EmployeeRegister;
import cn.bgs.service.EmpService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class EmpController {




    Logger log = LogManager.getLogger();

    @Autowired private EmpService empService;




   /* @RequestMapping("/findAll")
    public String findAll(@RequestParam(defaultValue = "1")Integer pageNum, Model m){

        Integer pageSize=2;

        PageInfo<EmployeeInfo> list= empService.findAll(pageNum,pageSize);

        m.addAttribute("p",list);

        return "list";

    }*/
    @RequestMapping("/login")
    public String login(Model m, HttpSession session,String email,String password){

        EmployeeRegister emp=empService.login(email,password);

        if(emp!=null){
            session.setAttribute("emp",emp);
            return "redirect:/start";
        }else {
           m.addAttribute("error","账号或者密码错误");
           return "login";
        }

    }
    @RequestMapping("/addEmp")
    public String addEmp(){

        return "add";

    }

    @RequestMapping("/add")
    public String add(EmployeeInfo employeeInfo){

       empService.add(employeeInfo);

        return "redirect:/findAll";


    }


    @RequestMapping("/zhuce")
    public String zhuce(){

        return "register";

    }




    @RequestMapping("/register")
    public String register(String username,String email,String password){
        log.info("register begin");
        log.info("register username: " + username);
        log.info("register email: " + email);
        log.info("register password: " + password);
        empService.register(username,email,password);


        return "login";


    }

  /*  @RequestMapping("/delete")
    public String delete(Integer[] ids){

        for( Integer pkId:ids){

            empService.delete(pkId);

        }
        return "redirect:/findAll";


    }*/



}
